package com.caishi.lkx.common;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzw.common.Wrappers;
import com.zzw.common.entity.BSService;
import com.zzw.common.entity.IBSEntity;
import com.zzw.common.exception.BizRuntimeException;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.List;

/**
 * @author by keray
 * date:2019/8/1 10:08
 */
public abstract class AdminBaseApi<S extends BSService<T, ID>, T extends IBSEntity<T, ID>, ID extends Serializable> extends BaseApi {

    public abstract S loadService();
    @PostMapping("/update")
    public T add(@RequestBody T entity)  {
        return loadService().insert(entity);
    }

    @DeleteMapping("/delete")
    public Boolean delete(@RequestParam ID id) {
        return loadService().delete(id);
    }

    @DeleteMapping("/deletes")
    public Boolean deletes(@RequestParam List<ID> ids) {
        return loadService().delete(ids);
    }

    @PutMapping("/update")
    public T update(@RequestBody T entity) {
        return loadService().simpleUpdate(entity);
    }

    @GetMapping("/detail/{id}")
    public T detail(@PathVariable ID id) {
        return loadService().getById(id);
    }

    @GetMapping("/page")
    public IPage<T> page(@ModelAttribute Page<T> page, @ModelAttribute T model, @NotNull JSONObject otherParam) {
        IPage<T> data = loadService().page(loadService().pageProcessing(page), baseWrapper(model, otherParam));
        data.setRecords(listCover(data.getRecords(), otherParam));
        return data;
    }

    @GetMapping("/list")
    public List<T> list(@RequestParam(defaultValue = "10") Integer size, @ModelAttribute T model, @NotNull JSONObject otherParam) {
        if (size > 1000) {
            size = 1000;
        }
        return listCover(loadService().selectList(loadService().wrapperProcessing(baseWrapper(model, otherParam)), size), otherParam);
    }


    @PostMapping("/details")
    public List<T> details(@RequestParam List<ID> ids) {
        return loadService().selectBatchIds(ids);
    }

    @GetMapping("/count")
    public Long count(@ModelAttribute T example) {
        if (BeanUtil.isEmpty(example)) {
            throw new BizRuntimeException(CommonResultCode.loadDataFail);
        }
        return loadService().selectCount(Wrappers.lambdaQuery(example));
    }

    @PutMapping("/change-sort")
    public Integer changeSort(@RequestParam ID id, @RequestParam Integer sort) {
        loadService().update(null, Wrappers.<T>update()
                .setSql("sort=" + sort)
                .eq("id", id)
        );
        return sort;
    }
    @PutMapping("/change-name")
    public String changeName(@RequestParam ID id, @RequestParam String name) {
        loadService().update(null, Wrappers.<T>update().setSql("name='"+name+"'").eq("id",id));

        return name;
    }

    protected LambdaQueryWrapper<T> baseWrapper(T model, JSONObject otherParam) {
        return Wrappers.lambdaQuery(model.clearEmptyStringField());
    }

    protected List<T> listCover(List<T> result, JSONObject otherParam) {
        return result;
    }

}
